﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dommel;//该库为Dapper扩展了crud方法
using Dapper;
using Bird.OAWeb.Domain.Table;
using Bird.OAWeb.EF;
using System.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

using Bird.OAWeb.Ropesitory;
/// <summary>
/// 仓储抽象基类
/// </summary>
/// <typeparam name="T"></typeparam>
public  class BaseRepository<T> : IBaseRopesitory<T> where T : class, new()
{
    //获取数据库连接对象 DapperSingleton这个类是第2步写的
    protected IDbConnection _db = DapperFactory.GetInstance().GetDbConnection();

    /// <summary>
    /// 添加一条实体数据
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int Inset(T entity)
    {

        return Convert.ToInt32(_db.Insert(entity));
    }
    /// <summary>
    /// 更新实体数据
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Update(T entity)
    {
        return _db.Update(entity);
    }
    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="entity">实体类</param>
    /// <returns></returns>
    public bool Delete(T entity)
    {
        return _db.Delete(entity);
    }
    /// <summary>
    /// 删除指定ID的数据
    /// </summary>
    /// <param name="objId"></param>
    /// <returns></returns>
    public bool DeleteById(object objId)
    {
        return Delete(_db.Get<T>(objId));
    }
    /// <summary>
    /// 根据主值查询单条数据
    /// </summary>
    /// <param name="objId">主键值</param>
    /// <returns>泛型实体</returns>
    public T GetById(object objId)
    {
        return _db.Get<T>(objId);
    }
    /// <summary>
    /// 获取全部数据
    /// </summary>
    /// <returns></returns>
    public List<T> GetList()
    {
        return _db.GetAll<T>().ToList();
    }
    
}